package com.jili.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jili.entity.User;
import com.jili.enums.CommonEnum;
import com.jili.mapper.UserMapper;
import com.jili.rsp.CommonRsp;
import com.jili.rsp.Result;
import com.jili.rsp.UserRsp;
import com.jili.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * ClassName: UserServiceImpl
 * Package com.jili.service.impl
 * Description:
 *
 * @Author: 廖继礼
 * @Create: 2025/2/23
 * @Version: v1.0
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    private final String KEY = "user:";

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private UserMapper userMapper;

    @Override
    public Result selectUserById(Integer id) {
        String json = stringRedisTemplate.opsForValue().get(KEY + id);
        User user = null;
        ObjectMapper objectMapper = new ObjectMapper();
        if (StringUtils.isNotBlank(json)) {
            try {
                user = objectMapper.readValue(json, User.class);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
            return new Result().ok(user);
        }
        user = userMapper.selectById(id);
        if (user != null) {
            try {
                String newJson = objectMapper.writeValueAsString(user);
                stringRedisTemplate.opsForValue().set(KEY + id, newJson);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
            return new Result().ok(user);
        }
        return new Result().ok("没有该值");
    }

    @Override
    public Result selectPage(Integer current, Integer size) {
        IPage<User> iPage = new Page<>(current, size);
        IPage<User> userIPage = userMapper.selectByPage(iPage);
        if (userIPage.getTotal() < 0) {
            return new Result().ok("没有该值");
        }
        UserRsp<User> userUserRsp = new UserRsp<>();
        long total = userIPage.getTotal();
        int count = (int) total / size;
        int reminder = (int) total % size;
        if (count > 0 && reminder > 0) {
            count++;
        }
        List<User> records = userIPage.getRecords();
        for (int i = 2; i <= count; i++) {
            iPage = new Page<>(i, size);
            IPage<User> userIPage1 = userMapper.selectByPage(iPage);
            records.addAll(userIPage1.getRecords());
        }
        userUserRsp.setData(records);
        userUserRsp.setTotal(total);
        return new Result().ok(userUserRsp);
    }

    @Override
    public Result selectWhile() {
        List<User> list = new ArrayList<>();
        int current = 1;
        while (true) {
            IPage<User> iPage = new Page<>(current, 2);
            List<User> records = userMapper.selectByPage(iPage).getRecords();
            if (CollectionUtils.isEmpty(records)) {
                break;
            }
            list.addAll(records);
            current++;
        }
        UserRsp<User> userUserRsp = new UserRsp<>();
        userUserRsp.setData(list);
        userUserRsp.setTotal((long) list.size());
        return new Result().ok(userUserRsp);
    }

    @Override
    public CommonRsp queryPage(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<User> userList = userMapper.selectList(null);
        PageInfo<User> pageInfo = new PageInfo(userList);
        UserRsp<User> userUserRsp = new UserRsp<>();
        userUserRsp.setTotal(pageInfo.getTotal());
        userUserRsp.setData(pageInfo.getList());
        return new CommonRsp() {
            {
                setObject(userUserRsp);
                setErrorCode(CommonEnum.SUCCESS.getCode());
                setErrorMessage(CommonEnum.SUCCESS.getMessage());
            }
        };
    }
}
